<?php namespace app\home\controller;

use system\model\Category;

class Listpage extends Common{
    //动作
    public function index($cid=null,$tid=null){
    //此处书写代码...

        if(!is_null($cid)){
            //获得该分类机器子类的cid组成的数组
            $data=Db::table('category')->get();
            $cids=(new Category())->findson($data,$cid);
            $cids[]=$cid;

            //拥有改分类及其子类有多少文章
            $countarticles[]=Db::table('article')->whereIn('cid',$cids)->count();
            $cname=Db::table('category')->where('cid',$cid)->first();
            $countarticles[]='类别为：'.$cname['cname'];

            //找出该分类及其所有子类的文章，并且不在回收站中
//            分页
            $page=Page::row(2)->make($countarticles[0]);
//            echo($page);
            $data=Db::table('article')
                ->join('category','article.cid','=','category.cid')
                ->field(['article.title','article.cid','article.aid','article.author','article.pubtime','category.cname','article.thumb','article.digest'])
                ->WhereIn('article.cid',$cids)
                ->limit(Page::limit())
                ->get();
            foreach($data as $k=>$v){
                $data[$k][tags]=Db::table('article')
                    ->join('article_tag','article.aid','=','article_tag.aid')
                    ->join('tag','tag.tid','=','article_tag.tid')
                    ->where('article_tag.aid',$v['aid'])
                    ->field(['tag.tid','tag.tname'])
                    ->get();
            }


        }
        if(!is_null($tid)){
            //拥有该标签的有多少文章,不在回收站的
            $countarticles[]=Db::table('article')
                ->join('article_tag','article.aid','=','article_tag.aid')
                ->where('article.recycle',0)
                ->where('article_tag.tid',$tid)
                ->count();
//            找出改标签名
                $tag=Db::table('article_tag')
                    ->join('tag','tag.tid','=','article_tag.tid')
                    ->where('tag.tid',$tid)
                    ->first();
                $tag=$tag['tname'];
               $countarticles[]='标签为：'.$tag;
//            p($countarticles);die;

//            分页
            $page=Page::row(2)->make($countarticles[0]);
            //找出拥有该标签的文章,不在回收站的
            $data=Db::table('article')->
                join('article_tag','article_tag.aid','=','article.aid')
                ->join('category','category.cid','=','article.cid')
                ->where('article.recycle',0)
                ->where('article_tag.tid',$tid)
                ->field(['article.title','article.cid','article.aid','article.author','article.pubtime','category.cname','article.thumb','article.digest'])
                ->get();
            foreach($data as $k=>$v){
                $data[$k]['tags']=[['tid'=>$tid,'tname'=>$tag]];
            }
        }
//        p($articles);
//        p($countarticles);
        $articles=['page'=>$page,'data'=>$data];
//        p($articles);die;
        View::with('countarticles',$countarticles);
        View::with('articles',$articles);
        View::with('cid',$cid);
        return view();
    }
}
